package com.ruoyi.web.controller.order;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;

import com.ruoyi.common.annotation.PropertyInject;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.InjectType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.order.domain.Order;
import com.ruoyi.order.service.IOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单Controller
 *
 * @author testlove
 * @date 2021-07-08
 */
@RestController
@RequestMapping("/system/order")
@Api("订单管理")
public class OrderController extends BaseController
{
    @Autowired
    private IOrderService orderService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询订单列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @GetMapping("/list")
    @ApiOperation("查询所有订单")
    public  TableDataInfo list(Order order)
    {
        startPage();
        List<Order> list = orderService.selectOrderList(order,tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getRoles());
        System.out.println("===============================================================");
        System.out.println(list);
        return getDataTable(list);
    }


    /**
     * 导出订单列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:export')")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ApiOperation("导出所有订单")
    public AjaxResult export(Order order)
    {
        SysUser user = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        List<SysRole> roles = user.getRoles();
        List<Order> list = orderService.selectOrderList(order,roles);
        ExcelUtil<Order> util = new ExcelUtil<Order>(Order.class);
        return util.exportExcel(list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") String orderId)
    {
        return AjaxResult.success(orderService.selectOrderById(orderId));
    }

    /**
     * 新增订单
     */
    @PreAuthorize("@ss.hasPermi('system:order:add')")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    @PropertyInject(type = InjectType.Insert)
    @ApiOperation("新增订单")
    public AjaxResult add(@RequestBody Order order)
    {

        try {
            return toAjax(orderService.insertOrder(order));
        }catch (Exception e) {
            if(e.getCause() instanceof SQLIntegrityConstraintViolationException) {
                return AjaxResult.error("订单编号重复！");
            }else return error("失败!");
        }


//        int rows = orderService.insertOrder(order);
//        if (rows > 0) {
//            return toAjax(rows);
//        }
//        return error("公司不存在");
    }

    /**
     * 修改订单
     */
    @PreAuthorize("@ss.hasPermi('system:order:edit')")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    @PropertyInject(type = InjectType.Update)
    public AjaxResult edit(@RequestBody Order order)
    {
        return toAjax(orderService.updateOrder(order));
    }

    /**
     * 删除订单
     */
    @PreAuthorize("@ss.hasPermi('system:order:remove')")
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    @ApiOperation("删除订单")
    public AjaxResult remove(@PathVariable String[] orderIds)
    {
        return toAjax(orderService.deleteOrderByIds(orderIds));
    }
}
